package com.qsign.sfrz_android.base;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.qsign.sfrz_android.publicview.WaitDialog;
import com.qsign.sfrz_android.utils.BaseUtil;
import androidx.core.view.ViewCompat;
import butterknife.ButterKnife;

import com.qsign.sfrz_android.R;
import com.zhy.autolayout.AutoLayoutActivity;

/**
 * Created by Administrator on 2019/8/14 0014.
 */

public abstract class NewBaseActivity extends AutoLayoutActivity {
    private WaitDialog mWaitDialog;

    protected String TAG = "";
    private Handler mHandler = new Handler();

    public boolean isActivityDestroyed() {
        return isActivityDestroyed;
    }

    public void setActivityDestroyed(boolean isActivityDestroyed) {
        this.isActivityDestroyed = isActivityDestroyed;
    }

    protected boolean isActivityDestroyed = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TAG = this.getClass().getName();
        // 设置界面无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 设置界面常亮
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mWaitDialog = new WaitDialog(this);

        setContentView(getContentViewId());
        ButterKnife.bind(this);
        setOrientation();
        initSystemBarTint();
        setAndroidNativeLightStatusBar(this, true);
        AppManager.getAppManager().addActivity(this);
        ImageView wgyback=(ImageView)this.findViewById(R.id.nav_back);
        if(wgyback!=null){
            wgyback.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
        TextView tvTitle=(TextView)this.findViewById(R.id.nav_title);
        if(tvTitle!=null){
            initTitle(tvTitle);
        }
        super.onCreate(savedInstanceState);

    }
    protected abstract int getContentViewId();
    protected abstract void initTitle(TextView textView);

    /**
     * 显示加载圈圈...
     */
    public void showWait() {
        mWaitDialog.show();
    }

    /**
     * 停掉加载圈圈...
     */
    public void stopWait() {
        mWaitDialog.dismiss();
    }

    public void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        isActivityDestroyed = true;
        AppManager.getAppManager().finishActivity(this);
        super.onDestroy();
    }

    protected Handler mMIPHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            int requestType = msg.what;
            int errorCode = msg.arg1;
            Object retObj = msg.obj;
            try {
                if (errorCode == 0) {

                    onPostHandle(requestType, retObj, true);

                } else {
                    onPostHandle(requestType, retObj, false);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.handleMessage(msg);
        }

    };

    protected void setOrientation() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    abstract public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception;
    /**
     * 子类可以重写决定是否使用透明状态栏
     * 默认不使用
     */
    protected boolean translucentStatusBar() {
        return false;
    }

    protected void initSystemBarTint() {
        Window window = getWindow();
        if (translucentStatusBar()) {
            // 设置状态栏全透明6.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//当系统版本为4.4或者4.4以上时可以使用沉浸式状态栏
                //透明状态栏
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //透明导航栏
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                setWindowStatusBarColor(R.color.colorWhite);
            }
            //return;
        }else{
            if (Build.VERSION.SDK_INT<Build.VERSION_CODES.M){//当系统版本为4.4-6.0使用沉浸式状态栏 因为6.0一下不能修改状态栏字体颜色
                //透明状态栏
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                //透明导航栏
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                setWindowStatusBarColor(R.color.colorWhite);
            }
        }
    }
    public void setWindowStatusBarColor(int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(colorResId));
                //底部导航栏
                //window.setNavigationBarColor(activity_home.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {//状态栏字体颜色为深色6.0以后
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decor = activity.getWindow().getDecorView();
            if (dark) {
                decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
        }
    }

    protected void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    protected void showKeyboard(EditText editText) {
        //InputMethodManager imm = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, 0);
    }
}
